package com.egao.orderB.biz.impl;

import com.egao.common.core.web.BaseDao;
import com.egao.common.core.web.PageResult2;
import com.egao.common.core.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.common.core.constants.UserConstant;
import com.egao.orderB.biz.DriverBiz;
import com.egao.orderB.entity.Driver;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-06-03 16:58:06
 * @version ：1.0
 */
@Repository
public class DriverBizImpl extends BaseDao implements DriverBiz {

	@Override
	public JSONObject list(PageResult2<T> page, Driver driver, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Driver> rm = new BeanPropertyRowMapper<Driver>(Driver.class);
		StringBuffer sql = new StringBuffer("select d.id,d.company_id,d.car_team");
		sql.append(",d.phone,d.name,d.id_card,d.car_no,d.vehicle_license,d.drive_license");
		sql.append(",d.corporation_license_pic,d.bank_name,d.bank_no,d.state");
		sql.append(",d.order_num,d.create_time,d.create_user,d.update_time");
		sql.append(",d.white_card,d.update_user");
		sql.append(",cmy.name companyName,suser.username createName ");
		sql.append(" from chf_driver d ");
		sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
		sql.append(" inner join chf_company cmy on cmy.id=d.company_id ");
		sql.append(" where d.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and d.company_id=:tablecompanyId ");
			map.put("tablecompanyId", loginUser.getCompanyId() + "");
		}
		if(Utils.isNotEmpty(driver)){
			if(Utils.isNotEmpty(driver.getName())){
				sql.append(" and instr(d.name,:name)>0 ");
				map.put("name", driver.getName());
			}
			if(Utils.isNotEmpty(driver.getCarNo())){
				sql.append(" and instr(d.car_no,:car_no)>0 ");
				map.put("car_no", driver.getCarNo());
			}
			if(Utils.isNotEmpty(driver.getWhiteCard())){
				sql.append(" and instr(d.white_card,:white_card)>0 ");
				map.put("white_card", driver.getWhiteCard());
			}
			if(Utils.isNotEmpty(driver.getState())){
				sql.append(" and instr(d.state,:state)>0 ");
				map.put("state", driver.getState());
			}
			if(Utils.isNotEmpty(driver.getSort())&&Utils.isNotEmpty(driver.getOrder())){
				sql.append(" order by "+driver.getSort()+" "+driver.getOrder());
			}else {
				sql.append(" order by d.create_time desc");
			}
		}
		return jsonPage2(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Driver> list(Driver driver, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Driver> rm = new BeanPropertyRowMapper<Driver>(Driver.class);
		StringBuffer sql = new StringBuffer("select d.id,d.company_id,d.car_team");
		sql.append(",d.phone,d.name,d.id_card,d.car_no,d.vehicle_license,d.drive_license");
		sql.append(",d.corporation_license_pic,d.bank_name,d.bank_no,d.state");
		sql.append(",d.order_num,d.create_time,d.create_user,d.update_time");
		sql.append(",d.white_card,d.update_user");
		sql.append(",cmy.name companyName,suser.username createName ");
		sql.append(" from chf_driver d ");
		sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
		sql.append(" inner join chf_company cmy on cmy.id=d.company_id ");
		sql.append(" where d.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and d.company_id=:tablecompanyId ");
			map.put("tablecompanyId", loginUser.getCompanyId() + "");
		}
		if(Utils.isNotEmpty(driver)){
			if(Utils.isNotEmpty(driver.getName())){
				sql.append(" and instr(d.name,:name)>0 ");
				map.put("name", driver.getName());
			}
			if(Utils.isNotEmpty(driver.getCarNo())){
				sql.append(" and instr(d.car_no,:car_no)>0 ");
				map.put("car_no", driver.getCarNo());
			}
			if(Utils.isNotEmpty(driver.getState())){
				sql.append(" and instr(d.state,:state)>0 ");
				map.put("state", driver.getState());
			}
			if(Utils.isNotEmpty(driver.getWhiteCard())){
				sql.append(" and instr(d.white_card,:white_card)>0 ");
				map.put("white_card", driver.getWhiteCard());
			}
			if(Utils.isNotEmpty(driver.getSort())&&Utils.isNotEmpty(driver.getOrder())){
				sql.append(" order by "+driver.getSort()+" "+driver.getOrder());
			}else {
				sql.append(" order by d.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Driver getById(Driver driver) {
		RowMapper<Driver> rm = new BeanPropertyRowMapper<Driver>(Driver.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id,d.company_id,d.car_team");
		sql.append(",d.phone,d.name,d.id_card,d.car_no,d.vehicle_license,d.drive_license");
		sql.append(",d.corporation_license_pic,d.bank_name,d.bank_no,d.state");
		sql.append(",d.order_num,d.create_time,d.create_user,d.update_time");
		sql.append(",d.white_card,d.update_user");
		sql.append(",cmy.name companyName,suser.username createName ");
		sql.append(" from chf_driver d ");
		sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
		sql.append(" inner join chf_company cmy on cmy.id=d.company_id ");
		sql.append(" where d.deleted=0 and cmy.deleted=0 and d.id=:id ");
		map.put("id", driver.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Driver driver, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_driver (");
		sql.append("company_id,phone,car_team,name,id_card,car_no,white_card");
		sql.append(",vehicle_license,drive_license,corporation_license_pic,bank_name");
		sql.append(",bank_no,state,order_num,create_user) values ( ");
		sql.append(":company_id,:phone,:car_team,:name,:id_card,:car_no,:white_card");
		sql.append(",:vehicle_license,:drive_license,:corporation_license_pic,:bank_name");
		sql.append(",:bank_no,:state,:order_num,:create_user)");
		map.put("company_id", loginUser.getCompanyId() + "");
		map.put("phone", driver.getPhone());
		map.put("car_team", driver.getCarTeam());
		map.put("name", driver.getName());
		map.put("id_card", driver.getIdCard());
		map.put("car_no", driver.getCarNo());
		map.put("white_card", driver.getWhiteCard());
		map.put("vehicle_license", driver.getVehicleLicense());
		map.put("drive_license", driver.getDriveLicense());
		map.put("corporation_license_pic", driver.getCorporationLicensePic());
		map.put("bank_name", driver.getBankName());
		map.put("bank_no", driver.getBankNo());
		map.put("state", driver.getState());
		map.put("order_num", driver.getOrderNum()==null?null:driver.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public Integer apiSave(Driver driver, User loginUser) {
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_driver (");
		sql.append("company_id,phone,car_team,name,id_card,car_no,white_card");
		sql.append(",vehicle_license,drive_license,corporation_license_pic,bank_name");
		sql.append(",bank_no,state,order_num,create_user) values ( ");
		sql.append(":company_id,:phone,:car_team,:name,:id_card,:car_no,:white_card");
		sql.append(",:vehicle_license,:drive_license,:corporation_license_pic,:bank_name");
		sql.append(",:bank_no,:state,:order_num,:create_user)");
		map.put("company_id", loginUser.getCompanyId() + "");
		map.put("phone", driver.getPhone());
		map.put("car_team", driver.getCarTeam());
		map.put("name", driver.getName());
		map.put("id_card", driver.getIdCard());
		map.put("car_no", driver.getCarNo());
		map.put("white_card", driver.getWhiteCard());
		map.put("vehicle_license", driver.getVehicleLicense());
		map.put("drive_license", driver.getDriveLicense());
		map.put("corporation_license_pic", driver.getCorporationLicensePic());
		map.put("bank_name", driver.getBankName());
		map.put("bank_no", driver.getBankNo());
		map.put("state", driver.getState());
		map.put("order_num", driver.getOrderNum()==null?null:driver.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		//		System.err.println("受到影响的行数=" + retVal + ",
		int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
			@Override
			public boolean hasValue(String paramName) {
				return map.containsKey(paramName);
			}
			@Override
			public Object getValue(String paramName) throws IllegalArgumentException {
				return map.get(paramName);
			}
		}, generatedKeyHolder);

		Integer id = generatedKeyHolder.getKey().intValue();
		return id;
	}

	@Override
	public boolean update(Driver driver, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver set ");
		sql.append("phone=:phone ");
		sql.append(",white_card=:white_card,car_team=:car_team ");
//		sql.append(",name=:name,id_card=:id_card,car_no=:car_no ");
		sql.append(",vehicle_license=:vehicle_license,drive_license=:drive_license,corporation_license_pic=:corporation_license_pic ");
		sql.append(",bank_name=:bank_name,bank_no=:bank_no");
		sql.append(",order_num=:order_num,update_user=:update_user where id=:id ");
		map.put("phone", driver.getPhone());
		map.put("white_card", driver.getWhiteCard());
		map.put("car_team", driver.getCarTeam());
//		map.put("name", driver.getName());
//		map.put("id_card", driver.getIdCard());
//		map.put("car_no", driver.getCarNo());
		map.put("vehicle_license", driver.getVehicleLicense());
		map.put("drive_license", driver.getDriveLicense());
		map.put("corporation_license_pic", driver.getCorporationLicensePic());
		map.put("bank_name", driver.getBankName());
		map.put("bank_no", driver.getBankNo());
		map.put("order_num", driver.getOrderNum()==null?null:driver.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driver.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean driverUpdate(Driver driver, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver set ");
		sql.append("phone=:phone ");
		sql.append(",white_card=:white_card ");
//		sql.append(",car_team=:car_team ");
//		sql.append(",name=:name,id_card=:id_card,car_no=:car_no ");
		sql.append(",vehicle_license=:vehicle_license,drive_license=:drive_license," +
				"corporation_license_pic=:corporation_license_pic ");
		sql.append(",bank_name=:bank_name,bank_no=:bank_no");
//		sql.append(",order_num=:order_num");
		sql.append(",update_user=:update_user where id=:id ");
		map.put("phone", driver.getPhone());
		map.put("white_card", driver.getWhiteCard());
//		map.put("car_team", driver.getCarTeam());
//		map.put("name", driver.getName());
//		map.put("id_card", driver.getIdCard());
//		map.put("car_no", driver.getCarNo());
		map.put("vehicle_license", driver.getVehicleLicense());
		map.put("drive_license", driver.getDriveLicense());
		map.put("corporation_license_pic", driver.getCorporationLicensePic());
		map.put("bank_name", driver.getBankName());
		map.put("bank_no", driver.getBankNo());
//		map.put("order_num", driver.getOrderNum()==null?null:driver.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driver.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean platUpdate(Driver driver, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver set ");
		sql.append("phone=:phone,white_card=:white_card ");
//		sql.append(",car_team=:car_team ");
		sql.append(",name=:name,id_card=:id_card,car_no=:car_no ");
//		sql.append(",vehicle_license=:vehicle_license,drive_license=:drive_license," +
//				"corporation_license_pic=:corporation_license_pic ");
//		sql.append(",bank_name=:bank_name,bank_no=:bank_no,order_num=:order_num");
		sql.append(",update_user=:update_user where id=:id ");
		map.put("phone", driver.getPhone());
		map.put("white_card", driver.getWhiteCard());
//		map.put("car_team", driver.getCarTeam());
		map.put("name", driver.getName());
		map.put("id_card", driver.getIdCard());
		map.put("car_no", driver.getCarNo());
//		map.put("vehicle_license", driver.getVehicleLicense());
//		map.put("drive_license", driver.getDriveLicense());
//		map.put("corporation_license_pic", driver.getCorporationLicensePic());
//		map.put("bank_name", driver.getBankName());
//		map.put("bank_no", driver.getBankNo());
//		map.put("order_num", driver.getOrderNum()==null?null:driver.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driver.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Driver driver, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(driver.getId().length()>0){
			for(String ID : driver.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_driver set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}

	@Override
	public boolean examine(Driver driver, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver set ");
		sql.append("state=:state,update_user=:update_user where id=:id ");
		map.put("state", driver.getState());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driver.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}
}
                                                                                                                                                                                                         